package com.pf.service.impl;

import com.pf.dto.AuditTaskDO;
import com.pf.dto.CheckFieldInfoDTO;
import com.pf.dto.ManualReminderReq;
import com.pf.dto.ManualReminderResp;
import com.pf.enums.FieldTypeEnum;
import com.pf.service.ManualReminderService;
import com.pf.utils.ManualReminderUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/11/21 20:27
 */
@Service
public class ManualReminderServiceImpl implements ManualReminderService {
    @Override
    public ManualReminderResp manualReminder(ManualReminderReq reminderReq) {

        Map<String, Integer> rowIndexAndCodeMap = reminderReq.getRowIndexAndCodeList().stream().collect(Collectors.toMap(ManualReminderReq.RowIndexAndCodeDTO::getCode, ManualReminderReq.RowIndexAndCodeDTO::getRowIndex));

        // 模拟查询审核任务数据
        List<CheckFieldInfoDTO> checkFieldInfoList = Lists.newArrayList();
        List<AuditTaskDO> auditTaskList = getAuditTaskList();
        for (int i = 0; i < auditTaskList.size(); i++) {
            AuditTaskDO auditTask = auditTaskList.get(i);
            int rowIndex = rowIndexAndCodeMap.get(auditTask.getTaskCode());
            CheckFieldInfoDTO info1 = CheckFieldInfoDTO.builder().rowIndex(rowIndex).fieldType(FieldTypeEnum.ACCOUNT).fieldDesc("迎审人").fieldValue(auditTask.getGreeter()).build();
            CheckFieldInfoDTO info2 = CheckFieldInfoDTO.builder().rowIndex(rowIndex).fieldType(FieldTypeEnum.ACCOUNT).fieldDesc("审核员").fieldValue(auditTask.getReviewPerson()).build();
            CheckFieldInfoDTO info3 = CheckFieldInfoDTO.builder().rowIndex(rowIndex).fieldType(FieldTypeEnum.STATUS).fieldDesc("状态").fieldValue(auditTask.getTaskStatus()).build();
            CheckFieldInfoDTO info4 = CheckFieldInfoDTO.builder().rowIndex(rowIndex).fieldType(FieldTypeEnum.OTHER).fieldDesc("下发状态").fieldValue(auditTask.getDistributionFlag()).errorMsg(checkDistributionFlag(auditTask.getDistributionFlag())).build();
            checkFieldInfoList.add(info1);
            checkFieldInfoList.add(info2);
            checkFieldInfoList.add(info3);
            checkFieldInfoList.add(info4);
        }
        // 提醒校验
        List<String> closeStatusList = Lists.newArrayList("Close");
        ManualReminderResp manualReminderResp = ManualReminderUtils.manualReminderVerification(checkFieldInfoList, closeStatusList);
        // 正式提醒
        System.out.println(">>>> 执行提醒");


        return manualReminderResp;
    }

    private String checkDistributionFlag(String distributionFlag) {
        if ("0".equals(distributionFlag)) {
            return "未下发的数据不能发送W3、Espace提醒；";
        }
        return null;
    }

    @Override
    public ManualReminderResp manualReminder2(ManualReminderReq reminderReq) {

        ManualReminderResp manualReminderResp = ManualReminderUtils.manualReminderVerification(reminderReq, () -> getAuditTaskList());
        return manualReminderResp;
    }

    private List<AuditTaskDO> getAuditTaskList() {
        AuditTaskDO task1 = AuditTaskDO.builder().taskCode("1001").greeter("wanger").reviewPerson("zhangsan").taskStatus("Open").distributionFlag("1").build();
        AuditTaskDO task2 = AuditTaskDO.builder().taskCode("1002").reviewPerson("zhangsan").taskStatus("Open").distributionFlag("0").build();
        AuditTaskDO task3 = AuditTaskDO.builder().taskCode("1003").greeter("wangwu2").reviewPerson("lisi").taskStatus("Close").build();
        AuditTaskDO task4 = AuditTaskDO.builder().taskCode("1004").greeter("wangwu").taskStatus("Open").build();
        return Lists.newArrayList(task1, task2, task3, task4);
    }
}
